﻿using AnesSystem.Controls;
using AnesSystem.Models.Model;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AnesSystem.ViewModels
{
    public class LaborEventAndMedicineCtrlViewModel :ViewModel
    {
        #region Variables

        private List<PatientLaborEventModel> _eventList;
        private ObservableCollection<FrameworkElement> _eventShowList;


        #endregion


        #region Constructor
        public LaborEventAndMedicineCtrlViewModel()
        {
            EventList = new List<PatientLaborEventModel>();
            EventShowList = new ObservableCollection<FrameworkElement>();
            InitData();
        }



        public List<PatientLaborEventModel> EventList
        {
            get
            {
                return _eventList;
            }

            set
            {
                _eventList = value;
            }
        }

        public ObservableCollection<FrameworkElement> EventShowList
        {
            get
            {
                return _eventShowList;
            }

            set
            {
                _eventShowList = value;
            }
        }

        public double FontSize
        {
            get
            {
                return PatientLink.LaborAnalgesia.LaborEventFontSize;
            }

            set
            {
                PatientLink.LaborAnalgesia.LaborEventFontSize = value;
            }
        }
        #endregion


        #region Properties


        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        private  void InitData()
        {
            EventList = PatientLaborEventService.Instance.GetPatientEvents8Time(PatientLink.Id.ToString(), PatientLink.Status.InLaborTime);
            CreatEventCtrl();
        }

        private void CreatEventCtrl()
        {
            if (EventList.Count > 0)
            {
                EventList.Sort((PatientLaborEventModel o1, PatientLaborEventModel o2) =>
                {
                    if (o1.BeginTime == o2.BeginTime)
                    {
                        if (o1.InputTime < o2.InputTime)
                        {
                            return -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                    else if (o1.BeginTime < o2.BeginTime)
                    {
                        return -1;
                    }
                    else if (o1.BeginTime > o2.BeginTime)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                });
                for (int i = 0; i < EventList.Count; i++)
                {
                    int index = i + 1;
                    EventAndDrugItemCtrl ctrl = new EventAndDrugItemCtrl();
                    ctrl.CurFontSize = FontSize;
                    string str = "";
                    StringBuilder builder = new StringBuilder();
                    if (index < 10)
                    {
                        str = "  " + index + "、" + EventList[i].BeginTime.ToString(DateFormat.DATE_FORMAT_STR_HM) + " ";
                        for (int j = 0; j < (str.Length - 1) * 2; j++)
                        {
                            builder.Append(" ");
                        }
                    }
                    else
                    {
                        str = index + "、" + EventList[i].BeginTime.ToString(DateFormat.DATE_FORMAT_STR_HM) + " ";
                        for (int j = 0; j < str.Length * 2; j++)
                        {
                            builder.Append(" ");
                        }
                    }


                    ctrl.Margin = new Thickness(0, 5, 10, 0);
                    ctrl.DisplayTime = str;
                    ctrl.DisplayName = builder.ToString() + EventList[i].Name;
                    ctrl.DataContext = EventList[i];
                    ctrl.MouseLeftButtonDown += Ctrl_MouseLeftButtonDown;
                    EventShowList.Add(ctrl);
                }
            }
        }

        private void Ctrl_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            EventAndDrugItemCtrl ctrl =sender as EventAndDrugItemCtrl;
            PatientLaborEventModel ple = ctrl.DataContext as PatientLaborEventModel;
            LaborEventUpdateCtrl laborEventUpdateCtrl = new LaborEventUpdateCtrl();
            laborEventUpdateCtrl.PatientLaborEvent = ple;
            DrugAndEventWindow.Instance.CusContent = laborEventUpdateCtrl;
            DrugAndEventWindow.Instance.Title = "修改事件";
            DrugAndEventWindow.Instance.ShowDialog();
        }

        #endregion


        #region Public/ProtectedMethods

        public void AddAndMinFontsize(bool isAdd)
        {
            try
            {
                bool flag = false;
                if (isAdd)
                {
                    if (FontSize < 30)
                    {
                        FontSize += 1;
                        flag = true;
                    }
                }
                else
                {
                    if (FontSize > 10)
                    {
                        FontSize -= 1;
                        flag = true;
                    }
                }
                if (flag)
                {
                    PatientLinkService.Instance.AddUpdate(PatientLink);
                    Refresh();
                }
            }
            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        public  void Refresh()
        {
            EventList.Clear();
            EventShowList.Clear();
            InitData();
        }
        #endregion


        #region EventHandlers


        #endregion
    }
}
